//
//  NearbyViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/15.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "NearbyViewController.h"

@interface NearbyViewController ()

@end

@implementation NearbyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"附近";
    self.allArray = [NSMutableArray array];
    self.nearbyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 20 - 44 - 49) style:UITableViewStylePlain];
    self.nearbyTableView.delegate = self;
    self.nearbyTableView.dataSource = self;

    [self.view addSubview:self.nearbyTableView];
    [self getDataValue];
    self.nextPage = 0;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];
    //下来刷新  上拉加载
    //    [self.recommendTableView.header beginRefreshing];
    [self headerRefesh];
    [self footerRefesh];
    
    
}

-(void)headerRefesh {
    self.nearbyTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.nextPage = 0;
        
        //让菊花旋转起来
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中...";
        [self.HUD show:YES];
        [self getDataValue];
        
    }];
    
}

-(void)footerRefesh {
    
    self.nearbyTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.nextPage++;
        //        self.downUpdata = NO;
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中...";
        [self.HUD show:YES];
        [self getDataValue];
        
    }];
    
}
-(void)getDataValue {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger num = 20 * self.nextPage;

    NSString *str_url = [NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?category=11&start=%ld&count=20&latitude=%@&longitude=%@", num, [userDefaults objectForKey:@"latitude"], [userDefaults objectForKey:@"longitude"]];
    NSLog(@"asdf = %@", str_url);
    if (self.nextPage == 0) {
        self.allArray = [NSMutableArray array];
        
    }
    [AFNGetDataValue getDataAFN:str_url block:^(NSMutableDictionary *dataDic) {
        

        NSMutableArray *array = [NSMutableArray arrayWithArray:[dataDic objectForKey:@"items"]];
        for (NSMutableDictionary *dic in array) {
            
            NearbyModle *nearby = [[NearbyModle alloc] init];
            [nearby setValuesForKeysWithDictionary:dic];
            nearby.placeId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];
            [self.allArray addObject:nearby];
            
            
        }
        
        NSLog(@"    = %ld", self.allArray.count);
        [self.nearbyTableView reloadData];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.nearbyTableView.footer endRefreshing];
        [self.nearbyTableView.header endRefreshing];
        [self.HUD hide:YES];
    }];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NearbyDetailViewController *nearbyD = [[NearbyDetailViewController alloc] init];
    NearbyModle *nearby = self.allArray[indexPath.row];
    nearbyD.placeId = nearby.placeId;
    [self.navigationController pushViewController:nearbyD animated:YES];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[NearbyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    NearbyModle *nearby = self.allArray[indexPath.row];
    cell.nearby = nearby;
//    cell.textLabel.text = @"1";
    return cell;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 180;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
